package com.wyh.aop;

import com.wyh.ann.SysLog;
import com.wyh.entity.LogInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @Author wangyaohan
 * @Date 2021/9/25 16:50
 * @ClassName SysLogAspect
 */
@Component
@Aspect
public class SysLogAspect {


    @Around("@annotation(sysLog)")
    public Object around(ProceedingJoinPoint joinPoint, SysLog sysLog) {
        Object value = null;
        try {
            //包名和类名 例如：com.wyh.controlller.InsuranceController
            String className = joinPoint.getTarget().getClass().getCanonicalName();
            //类名 例如：InsuranceController
            //String className = joinPoint.getTarget().getClass().getSimpleName();
            //方法签名
            String methodName = joinPoint.getSignature().getName();
            //方法参数
            Object[] args = joinPoint.getArgs();
            //日志对象
            LogInfo logInfo = new LogInfo();
            logInfo.setClassName(className);
            logInfo.setMethodName(methodName);
            logInfo.setArgs(args);
            logInfo.setDoThing(sysLog.value());
            /**
             * //打印 前置通知
             */
            System.err.println("前置通知：" + logInfo);
            //让目标方法执行
            value = joinPoint.proceed();
            /**
             * 后置通知
             */
            System.err.println("后置通知：" + logInfo);
        } catch (Throwable throwable) {
            //异常通知
            throwable.printStackTrace();
        } finally {
            //最终通知
        }
        return value;
    }
}
